using Autofac;
using Lcygcc.Application.WCF.Service;
using Lcygcc.Domain.Repository.Entity;
using Lcygcc.Domain.Service;
using Lcygcc.Infrastructure.Core;
using Lcygcc.Infrastructure.Logs;
using Lcygcc.Infrastructure.Repository;
using Lcygcc.Infrastructure.Utility.IOC;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.OracleClient;
using System.Reflection;
namespace Lcygcc.WindowService.Host
{
	public class ServiceManage
	{
		public static void Start()
		{
            // 启用日志
            Logger.Instance().Register();

            Logger.Info("----IOC注册开始----");
            ///IOC注册
            EngineContext.Initialize(false);

            Logger.Info("----IOC注册完成----");

            //Logger.Info("----WCF启动开始----");

            ////WCFServiceHost serviceHost = WCFServiceHost.Instance();

            ////serviceHost.StartServices();


            //Logger.Info("----WCF启动完成----");

            Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            EnvironmentInfo.JKBaseUrl = configuration.AppSettings.Settings["JKServiceUri"].Value;

            #region 加载字典

            try
            {

                var baseRepository = EngineContext.Current.Resolve<IBaseRepository>();
                var dicTypeList = baseRepository.db.Queryable<DicType>().Where(it => it.Valid == 1).ToList();
                foreach (var dicType in dicTypeList)
                {
                    DictionaryInfo info = new DictionaryInfo();
                    info.dicDetailList = new List<DicDetail>();
                    info.dicType = dicType;
                    List<DicDetail> dicDetailList = baseRepository.db.Queryable<DicDetail>().Where(it => it.Valid == 1 && it.DicTypeId == dicType.DicTypeId).ToList();
                    foreach (DicDetail dicDetail in dicDetailList)
                    {
                        info.dicDetailList.Add(dicDetail);
                    }
                    EnvironmentInfo.DictionaryInfoList.Add(info);
                }
            
            #endregion 加载字典

            #region 加载定时任务
            var taskCustomInfoList = baseRepository.db.Queryable<Infrastructure.Core.TaskCustomInfo>().ToList();
            EnvironmentInfo.TaskCustomInfoList = taskCustomInfoList;
                #endregion 加载定时任务
            }
            catch (Exception ex)
            {
                Logger.Error("--------ServiceManage.Start--------",ex);
                throw ex;
            }
            Logger.Info("----IOC初始化开始----");
            EngineContext.Current.InitTask();
            Logger.Info("----IOC初始化完成----");

            //TestOracle();

            //var roleDB = EngineContext.Current.Resolve<IRepository<OperatorInfo>>();
            //var role = roleDB.db.Queryable<OperatorInfo>().ToList();
            //roleDB.Insert(new OperatorInfo() { Id = 9,Name = "9996",Remark = "研发人员"});

        }
		public static void Stop()
		{
            //MiddleManager m = Factory.IContainer.Resolve<MiddleManager>();
            //m.Stop();
            //UserManager u = Factory.IContainer.Resolve<UserManager>();
            //u.Stop();
           // WebApiService webApiService = Factory.IContainer.Resolve<WebApiService>();
			//webApiService.Stop();
		//	JSEY09 jSEY = Factory.IContainer.Resolve<JSEY09>();
		//	jSEY.Stop();
		}

        private static void TestOracle()
        {
            IPersonService m_PersonService = EngineContext.Current.Resolve<IPersonService>();
            m_PersonService.GetPersonsByOracle();
            //try
            //{
            //    using (OracleConnection conn = Lcygcc.DataBase.Entities.OracleHelper.GetOracleConnectionAndOpen)
            //    {
            //        if (conn.State == ConnectionState.Open)
            //        {
            //            Console.WriteLine("打开连接成功！");
            //        }
            //    }
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine("出现异常, 异常信息: " + ex.Message);
            //}
        }
	}
}
